/*
 * gy_enpclient.cpp
 *
 *  Created on: 2015年9月15日
 *      Author: gaoyi
 */


#include "gy_enpclient.h"
#include "gy_enpctrl.h"

int GYEnpClient::Recv()
{
	int iRecvLen = 0;
	int iRet = 0;

	iRet = m_stSocket.Recv(m_stRecvBuf.GetFreeBuf(), m_stRecvBuf.GetFreeLen(), iRecvLen);

	if (iRecvLen > 0)
	{
		m_stRecvBuf.Append(iRecvLen);
	}

	return iRet;
}

int GYEnpClient::Init(GYEnpCtrl& stOwner, GYEnpListen& stListen,
	SOCKET iSocket, unsigned int uiIP, unsigned short unPort)
{
	m_pstOwner = &stOwner;
	m_pstListen = &stListen;
	m_stSocket.SetSocket(iSocket);

	int iRet = Register(stOwner, &GYEnpCtrl::OnClientEvent, stOwner.m_stEpoll, m_stSocket.GetSocket(), EPOLLIN);
	CHECK_THROW(iRet);

	const GYEnpListenInfo& stListenInfo = m_pstListen->GetListenInfo();
	m_stNetHead.uiLocalIP = inet_addr(stListenInfo.m_szListenIP.c_str());
	m_stNetHead.unLocalPort = stListenInfo.m_unListenPort;
	m_stNetHead.uiRemoteIP = uiIP;
	m_stNetHead.unRemotePort = unPort;

    m_stNetHead.uiLastTime = stOwner.m_uiNow;
    m_stNetHead.uiCreateTime = stOwner.m_uiNow;
    m_stNetHead.u64Act1 = 0;
    m_stNetHead.u64Act2 = 0;
    m_stNetHead.unLiveFlag = 0;
    m_stNetHead.ucEnodeMethod = stListenInfo.m_ucEncodeMethod;
    m_stNetHead.ucDeodeMethod = stListenInfo.m_ucDecodeMethod;
    m_stNetHead.szPtKey[0] = '\0';

	m_stRecvBuf.Attach(NULL,GY_ENP_CLIENT_BUFFER_SIZE,0);
	m_stSendBuf.Attach(NULL,GY_ENP_CLIENT_BUFFER_SIZE,0);
    return 0;
}

